import { reqLogin , reqUserInfo, reqLogout } from '@/api/user/index'
import {setToken , getToken, removeToken} from '@/utils/token'
const state = {
  token:getToken(),
  userInfo:{}
}
const mutations = {
  GOLOGIN(state,token){
    state.token = token
  },
  FINDINFO(state,userInfo){
    state.userInfo=userInfo
  },
  CLEAR(state){
    state.token = '',
    state.userInfo = {}
    removeToken()
  }
}
const actions = {
  // 登录的回调函数 18944444444 123
  async goLogin({commit},data){
    let res = await reqLogin(data)
    if(res.code == 200){
      commit('GOLOGIN',res.data.token)
      setToken(res.data.token)
      return 'ok'
    }else{
      return Promise.reject(new Error('fail'))
    }
  },
  // 获取登录信息
  async getUserInfo({commit}){
    let res = await reqUserInfo()
    if(res.code == 200){
      commit('FINDINFO',res.data)
    }
  },
  // 退出登录
  async userLogout({commit}){
    let res = await reqLogout()
    if(res.code == 200){
      commit('CLEAR')
      return 'ok'
    }else{
      Promise.reject(new Error('fail'))
    }
  }
}
const getters = {

}

export default{
  state,
  mutations,
  actions,
  getters
}